﻿using Inovout.Memberships;
using Inovout.Memberships.Models;
using Inovout.People.Memberships.Models;
using Inovout.People.Memberships.Repositories;
using Inovout.People.Models;
using Inovout.Security;
using Inovout.Security.Services;
using Inovout.Services;

namespace Inovout.People.Memberships.Services
{
    //实现类
    public class PersonMembershipService : RepositoryService<PersonMembership>, IPersonMembershipService
    {
        protected PersonMembershipRepository PersonMembershipRepository
        {
            get { return (PersonMembershipRepository)base.Repository; }
        }

        private IFormsUserCredentialService formUserCredentialService;
        private IRepositoryService<OpenIdUserCredential> openIdUserCredentialService;
        private IRepositoryService<OAuthUserCredential> oAuthUserCredentialService;
        private IRepositoryService<Person> personService;
        private IRepositoryService<MembershipUser> membershipUserService;
        private IRoleService roleService;
        public PersonMembershipService(IRepositoryService<Person> personService,
            IFormsUserCredentialService formUserCredentialService, IRepositoryService<MembershipUser> membershipUserService,
            IRoleService roleService,
            IRepositoryService<OpenIdUserCredential> openIdUserCredentialService,
            IRepositoryService<OAuthUserCredential> oAuthUserCredentialService)
        {
            this.personService = personService;
            this.formUserCredentialService = formUserCredentialService;
            this.membershipUserService = membershipUserService;
            this.roleService = roleService;
            this.openIdUserCredentialService = openIdUserCredentialService;
            this.oAuthUserCredentialService = oAuthUserCredentialService;
        }
        public IMembershipUser Create(Person person, UserCredential userCredential, MembershipUserStatus status)
        {
            PersonMembership personMembership = null;
            if (userCredential is OpenIdUserCredential)
            {
                openIdUserCredentialService.Save((OpenIdUserCredential)userCredential);
            }
            if (userCredential is FormsUserCredential)
            {
                formUserCredentialService.Create((FormsUserCredential)userCredential);
            }
            if (userCredential is OAuthUserCredential)
            {
                oAuthUserCredentialService.Save((OAuthUserCredential)userCredential);
            }
            personMembership = new PersonMembership(person);
            base.Save(personMembership);
            var membershipUser = new MembershipUser(personMembership, userCredential.SystemUser, person.Email, status);
            membershipUserService.Save(membershipUser);

            return membershipUser;
        }


        public PersonMembership FindByUserName(string userName)
        {
            return PersonMembershipRepository.FindByUserName(userName);
        }
    }
}
